package cn.yikaojiuguo.student.ui.ask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.etsy.android.grid.StaggeredGridView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.adapter.AskChooseCourseAdapter;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.bean.Category;
import cn.yikaojiuguo.student.support.database.MyDBManager;
import cn.yikaojiuguo.student.support.simple.ArgsConstants;
import cn.yikaojiuguo.student.support.utils.LogUtil;
import cn.yikaojiuguo.student.ui.base.CustomerActionBarActivity;

public class AskChooseSecondMajorActivity extends CustomerActionBarActivity {

    @Bind(R.id.gv)
    StaggeredGridView mGv;
    @Bind(R.id.tv_change)
    TextView mTv_change;
    AskChooseCourseAdapter mAdapter;
    private final static int CHOOSE_MAJOR = 1;
    private List<String> mPics;
    private int mType = AskChooseCourseAdapter.UPLOAD_WORD;
    private String mContent;
    private String mAudiotime;

    public static Intent newIntent(String content) {
        Intent intent = new Intent(YiKaoApplication.getInstance(),AskChooseSecondMajorActivity.class);
        intent.putExtra(ArgsConstants.TYPE,AskChooseCourseAdapter.UPLOAD_WORD);
        intent.putExtra(ArgsConstants.CONTENT,content);
        return intent;
    }

    public static Intent newPicsIntent(List<String> pics,String content) {
        Intent intent = new Intent(YiKaoApplication.getInstance(),AskChooseSecondMajorActivity.class);
        intent.putStringArrayListExtra(ArgsConstants.PICS, (ArrayList<String>) pics);
        intent.putExtra(ArgsConstants.TYPE, AskChooseCourseAdapter.UPLOAD_PICS);
        intent.putExtra(ArgsConstants.CONTENT,content);
        return intent;
    }

    public static Intent newVoiceIntent(String content,String audiotime){
        Intent intent = new Intent(YiKaoApplication.getInstance(),AskChooseSecondMajorActivity.class);
        intent.putExtra(ArgsConstants.TYPE,AskChooseCourseAdapter.UPLOAD_VOICE);
        intent.putExtra(ArgsConstants.CONTENT,content);
        intent.putExtra(ArgsConstants.LONG_TIME,audiotime);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ask_choose_second_major);
        ButterKnife.bind(this);
        mPics = getIntent().getStringArrayListExtra(ArgsConstants.PICS);
        mType = getIntent().getIntExtra(ArgsConstants.TYPE, AskChooseCourseAdapter.UPLOAD_WORD);
        mContent = getIntent().getStringExtra(ArgsConstants.CONTENT);
        mAudiotime = getIntent().getStringExtra(ArgsConstants.LONG_TIME);
        initView();
        initEvents();
    }

    private void initEvents() {
        mTv_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(AskChooseMajorActivity.newIntent(),CHOOSE_MAJOR);
            }
        });
    }

    private void initView() {
        changeData(YiKaoApplication.getMajorId());

    }

    private void changeData(String majorId) {
        List<Category> list = MyDBManager.queryByEvalue(Integer.parseInt(majorId));
        if(mAdapter == null) {
            mAdapter = new AskChooseCourseAdapter(this,list,mType,mPics,mContent,mAudiotime);
            mGv.setAdapter(mAdapter);
        } else {
            mAdapter.addFirstPageData(list);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == CHOOSE_MAJOR && resultCode == Activity.RESULT_OK) {
            LogUtil.showInfoLog("id","major id = " + data.getStringExtra(ArgsConstants.MAJOR_ID));
            changeData(data.getStringExtra(ArgsConstants.MAJOR_ID));
        }
    }
}
